<?php
/* Mailer with Attachments */

$action = "";
$action = $_REQUEST['action'];
global $action;

function showForm() {
?>

<form enctype="multipart/form-data" name="send" method="post" action="<?=$_SERVER['PHP_SELF']?>">
<input type="hidden" name="action" value="send" />
<input type="hidden" name="MAX_FILE_SIZE" value="10000000" />
<p>Recipient Name: <input name="to_name" size="50" /><br />
Recipient Email: <input name="to_email" size="50" /><br />
From Name: <input name="from_name" size="50" /><br />
From Email: <input name="from_email" size="50" /><br />
Subject: <input name="subject" size="50" /><br />
Message: <textarea name="body" rows="10" cols="50"></textarea><br />
Attachment: <input type="file" name="attachment" size="50" /><br />
<input type="submit" value="Send Email" /></p>

<?php
}
function sendMail()
{
	if (!isset ($_POST['to_email'])) { //Oops, forgot your email addy!
	die ("<p>Oops! You forgot to fill out the email address! Click on the back arrow to go back</p>");
	} 
	else {
	$to_name = stripslashes($_POST['to_name']);
	$from_name = stripslashes($_POST['from_name']);
	$subject = stripslashes($_POST['subject']);
	$body = stripslashes($_POST['message]);
	$to_email = $_POST['to_email'];
	
	//Let's start our headers
	$headers = "From: $from_name <" . $_POST['from_email'] . ">\n";
	$headers .= "Reply-To: <" . $_POST['from_email'] . ">\n";
	$headers .= "MIME-Version: 1.0\n";
	$headers .= "Content-Type: multipart/related; type=\"multipart/alternative\"; boundary=\"----=MIME_BOUNDRY_main_message\"\n";
	$headers .= "X-Sender: $from_name <" . $_POST['from_email'] . ">\n";
	$headers .= "X-Mailer: PHP4\n";
	//
	//if (checkbox_form.checkbox[counter].checked)
	//Return-Receipt-To: x@x.com
	//X-Confirm-Reading-To: x@x.com
	//Disposition-Notification-To: x@x.com
	//
	$headers .= "X-Priority: 3\n"; //1 = Urgent, 3 = Normal
	$headers .= "Return-Path: <" . $_POST['from_email'] . ">\n";
	$headers .= "This is a multi-part message in MIME format.\n";
	$headers .= "------=MIME_BOUNDRY_main_message \n";
	$headers .= "Content-Type: multipart/alternative; boundary=\"----=MIME_BOUNDRY_message_parts\"\n";
	$message = "------=MIME_BOUNDRY_message_parts\n";
	$message .= "Content-Type: text/plain; charset=\"iso-8859-1\"\n";
	$message .= "Content-Transfer-Encoding: quoted-printable\n";
	$message .= "\n";
	/* Add our message, in this case it's plain text. You could also add HTML by changing the Content-Type to text/html */
	$message .= "$body\n";
	$message .= "\n";
	$message .= "------=MIME_BOUNDRY_message_parts--\n";
	$message .= "\n";
	
	if($file != "") {
		foreach($_FILES as $file => $value) {
		$_tmpname = $_FILES[$file]['tmp_name'];
		$_filename = $_FILES[$file]['name'];
			if (is_uploaded_file($_tmpname)) { //Do we have a file uploaded?
			$fp = fopen($_tmpname, "rb"); //Open it
			$data = fread($fp, filesize($_tmpname)); //Read it
			$data = chunk_split(base64_encode($data)); //Chunk it up and encode it as base64 so it can emailed
			$message .= "------=MIME_BOUNDRY_main_message\n";
			$message .= "Content-Type: application/octet-stream;\n\tname=\"" . $_filename . "\"\n";
			$message .= "Content-Transfer-Encoding: base64\n";
			$message .= "Content-Disposition: attachment;\n\tfilename=\"" . $_filename . "\"\n\n";
			$message .= $data; //The base64 encoded message
			$message .= "\n\n";
			fclose($fp);
			}
		}
	}
	
	$message .= "------=MIME_BOUNDRY_main_message--\n";
	
	// send the message
	
	$ok = mail("$to_name <$to_email>", $subject, $message, $headers);
	
		if ($ok == 1) {
		print "Mail sent to $to_name ($to_email).";
		} else {
		print "Mail did not send. Please press the <b>back</b> button and re-submit the e-mail.";
		}
	}
}

?>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>

</head>
<body>

<?php
switch ($action) {
case "send":
sendMail();
/*showForm();*/
break;
default:
showForm();
}
?>

</body>
</html>